<template>
  <div class="autumn-container">
    <!-- 面包 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/dashboard' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/metadata/productInfo/productInfoList' }">商品信息列表</el-breadcrumb-item>
      <el-breadcrumb-item>商品信息</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <div class="autumn-center">
      <div class="edit-form">
        <el-form ref="editFormRef" size="large" label-width="90px" :model="editForm" :rules="editFormRules">

          <el-row>
            <el-col :span="24">
              <el-form-item label="封面">
                <autumn-avatar v-model="editForm.coverImg"></autumn-avatar>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="分类" prop="category">
                <autumn-tree-select :data="treeCategorys" v-model="editForm.category" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="编码" prop="code">
                <el-input v-model="editForm.code" :disabled="editForm.id ? true : false" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="名称" prop="name">
                <el-input v-model="editForm.name" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="型号" prop="model">
                <el-input v-model="editForm.model" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="规格" prop="spec">
                <el-input v-model="editForm.spec" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="单位" prop="unit">
                <el-select v-model="editForm.unit" filterable reserve-keyword>
                  <el-option v-for="item in perUnits" :key="item.id" :label="item.code + '-' + item.name"
                    :value="item.id" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="售价" prop="unitPrice">
                <el-input-number v-model="editForm.unitPrice" :min="0.01" :max="99999999.99" :precision="2" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="图片">
                <autumn-upload ref="uploadRef" :multiple="true" module="商品信息" :businessId="editForm.id"></autumn-upload>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="详情" prop="remark">
                <!-- <el-input type="textarea" resize="none" v-model="editForm.remark" /> -->
                <autumn-editor v-model:valueHtml="editForm.remark"></autumn-editor>
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>

        <div class="form-foot-button">
          <el-button type="primary" @click="submit(editFormRef)">保存</el-button>
          <el-button @click="cancel">取消</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { FormInstance } from 'element-plus'
import { $convertTree, $listPerUnit } from 'src/util/tools'
import { viewProductInfo, saveProductInfo, updateProductInfo } from 'src/api/metadata/productInfo'
import { listProductCategory } from 'src/api/metadata/productCategory'

// 查询商品分类
const treeCategorys = ref<Array<any>>([])
listProductCategory().then(({ data }) => {
  treeCategorys.value = $convertTree(data)
})
//查询计量单位
const perUnits = ref<Array<any>>([])
perUnits.value = $listPerUnit()

console.log('perUnits.value == ', perUnits.value);

// 定义表单数据
const editFormRef = ref<FormInstance>()
const editForm = reactive({
  id: '',
  category: '',
  name: '',
  code: '',
  spec: '',
  model: '',
  coverImg: '',
  unit: '',
  unitPrice: 0.01,
  remark: ''
})

//定义预览
const previewList = ref<Array<string>>([])

const editFormRules = {
  category: [
    { required: true, message: '请选择商品分类', trigger: ['blur', 'change'] }
  ],
  code: [
    { required: true, message: '请输入商品编码', trigger: 'blur' }
  ],
  name: [
    { required: true, message: '请输入商品名称', trigger: 'blur' }
  ],
  coverImg: [
    { required: true, message: '请上传封面图片', trigger: 'blur' }
  ]
  // ,
  // unit: [
  //   { required: true, message: '请选择单位', trigger: ['blur', 'change'] }
  // ]
}

// 修改时填充表单数据
const route = useRoute()
if (route.query.id) {
  const id = route.query.id as string
  viewProductInfo(id).then(({ data }) => {
    Object.assign(editForm, data)
  })
}

// 提交
const submit = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(valid => {
    if (!valid) return
    if (!editForm.id) {
      saveProductInfo(editForm).then(() => {
        cancel()
      })
    } else {
      updateProductInfo(editForm).then(() => {
        cancel()
      })
    }
  })
}

// 取消
const router = useRouter()
const cancel = () => {
  router.push('/metadata/productInfo/productInfoList')
}

</script>

<style lang="scss" scoped></style>
